package top.stacktrace.BookInfo.service.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import top.stacktrace.BookInfo.domain.Book;
import top.stacktrace.BookInfo.domain.ReserveBO;
import top.stacktrace.BookInfo.service.BookService;
import top.stacktrace.BookInfo.utils.Result;

@RestController
public class BookController {
    @Autowired
    private BookService bookService;

    @PostMapping("/addbook")
    public Result addBook(@RequestBody Book book) {
        return Result.get(bookService.addBook(book),"add book failed","");
    }

    @GetMapping("/books")
    public Result getBooks(){
        return Result.ok(bookService.getBooks());
    }

    @GetMapping("/book")
    public Result getBookDetail(@RequestParam int id){
        return Result.ok(bookService.getBookDetail(id));
    }

    @PostMapping("/book/reserve/set")
    public Result setBookReserve(@RequestBody ReserveBO bo){
        return Result.get(bookService.setBookReserve(bo.getBookid(),bo.getReserve()),"set reserve failed","");
    }

    @PostMapping("/book/reserve/add")
    public Result inCreaseReserve(@RequestBody ReserveBO bo){
        return Result.get(bookService.inCreaseReserve(bo.getBookid(),bo.getQuantity()),"increase reserve failed","");
    }

    @PostMapping("/book/reserve/sub")
    public Result deCreaseReserve(@RequestBody ReserveBO bo){
        return Result.get(bookService.reserve(bo.getBookid(),bo.getQuantity()),"decrease reserve failed","");
    }
}
